package com.gtfrank.web.servlet.day3;

import com.gtfrank.web.servlet.pojo.User;
import com.gtfrank.web.servlet.service.IUserService;
import com.gtfrank.web.servlet.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;

/**
 * @ProjectName: sudacoursetrainging
 * @ClassName: ShowUserListServlet
 * @PackageName:com.gtfrank.web.servlet.day3
 * @Description:
 * @Author: frank
 * @Date: 2021/4/29 14:01
 * @Version v1.0
 */
@WebServlet(value = "/user/showUserListServlet")
public class ShowUserListServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("utf-8");
        PrintWriter out = resp.getWriter();
        out.println("<!DOCTYPE html>");
        out.println("<html lang=\"en\">");
        out.println("<head>");
        out.println("    <meta charset=\"UTF-8\">");
        out.println("    <title>Title</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("    <h1 align=\"center\">View User List</h1>");
        out.println("    <hr>");
        out.println("    <center>");
        out.println("    <form method=\"post\" action=\"/javaeeproject/user/SerachUserServlet\">");
        out.println("    请输入搜索的名字：<input type=\"test\" name=\"userName\"> <input type=\"submit\" value=\"serach\"><br>");
        out.println("    </center>");
        out.println("    <hr>");
        out.println("</form>");
        out.println("    <table border=\"1\" bgcolor=\"#7fffd4\" width=\"800px\" align=\"center\">");
        out.println("        <tr>");
        out.println("            <td>ID</td>");
        out.println("            <td>用户名</td>");
        out.println("            <td>密码</td>");
        out.println("            <td>操作</td>");
        out.println("        </tr>");
        IUserService userService = new UserServiceImpl();
        List<User> userList = userService.queryAll();
        Iterator<User> iterator = userList.iterator();
        while(iterator.hasNext()){
            User user = iterator.next();
            out.println("        <tr>");
            out.println("            <td>"+user.getId()+"</td>");
            out.println("            <td>"+user.getName()+"</td>");
            out.println("            <td>"+user.getPassword()+"</td>");
            out.println("            <td><a href=\"/javaeeproject/user/UserToModifyServlet?userid="+user.getId()+"\">修改</a>|<a href=\"/javaeeproject/user/userDeleteServlet?userid="+user.getId()+"\">删除</a></td>");
            out.println("        </tr>");
        }
        out.println("");
        out.println("    </table>");
        out.println("</body>");
        out.flush();
        out.close();

    }
}
